#!/bin/bash

# 鱼C论坛闪拍监控 - 状态检查脚本
# 作者: SOLO Coding
# 用途: 检查监控程序运行状态

# 获取脚本所在目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

# 定义文件路径
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
PID_FILE="$PROJECT_DIR/fishc_monitor.pid"
LOG_FILE="$PROJECT_DIR/logs/fishc_monitor.log"
STATE_FILE="$PROJECT_DIR/data/monitor_state.json"
CONFIG_FILE="$PROJECT_DIR/config/config.json"

echo "=== 鱼C论坛闪拍监控 - 状态检查 ==="
echo "检查时间: $(date '+%Y-%m-%d %H:%M:%S')"
echo "工作目录: $SCRIPT_DIR"
echo ""

# 检查进程状态
echo "📋 进程状态:"
if [ -f "$PID_FILE" ]; then
    PID=$(cat "$PID_FILE")
    if ps -p "$PID" > /dev/null 2>&1; then
        echo "  ✅ 监控程序正在运行 (PID: $PID)"
        
        # 显示进程信息
        echo "  📊 进程信息:"
        ps -p "$PID" -o pid,ppid,cmd,etime,pcpu,pmem --no-headers | while read line; do
            echo "     $line"
        done
        
        # 显示进程启动时间
        START_TIME=$(ps -p "$PID" -o lstart --no-headers 2>/dev/null || echo "未知")
        echo "  🕐 启动时间: $START_TIME"
    else
        echo "  ❌ PID文件存在但进程不在运行 (PID: $PID)"
        echo "  🧹 建议清理: rm $PID_FILE"
    fi
else
    echo "  ⚪ 未找到PID文件，监控程序可能未在后台运行"
fi

echo ""

# 检查配置文件
echo "⚙️  配置状态:"
if [ -f "$CONFIG_FILE" ]; then
    echo "  ✅ 配置文件存在: $CONFIG_FILE"
    
    # 如果有jq，显示关键配置
    if command -v jq &> /dev/null; then
        echo "  📋 关键配置:"
        URL=$(jq -r '.url // "未设置"' "$CONFIG_FILE")
        SCHEDULE_ENABLED=$(jq -r '.schedule.enabled // false' "$CONFIG_FILE")
        SCHEDULE_TIME=$(jq -r '.schedule.time // "未设置"' "$CONFIG_FILE")
        CHECK_INTERVAL=$(jq -r '.check_interval // "未设置"' "$CONFIG_FILE")
        EMAIL_ENABLED=$(jq -r '.email.enabled // false' "$CONFIG_FILE")
        WECHAT_ENABLED=$(jq -r '.wechat.enabled // false' "$CONFIG_FILE")
        
        echo "     🌐 监控URL: $URL"
        echo "     🕘 定时调度: $SCHEDULE_ENABLED ($SCHEDULE_TIME)"
        echo "     ⏱️  检查间隔: $CHECK_INTERVAL 秒"
        echo "     📧 邮件通知: $EMAIL_ENABLED"
        echo "     💬 微信通知: $WECHAT_ENABLED"
    fi
else
    echo "  ⚠️  配置文件不存在: $CONFIG_FILE"
fi

echo ""

# 检查状态文件
echo "💾 监控状态:"
if [ -f "$STATE_FILE" ]; then
    echo "  ✅ 状态文件存在: $STATE_FILE"
    
    # 如果有jq，显示状态信息
    if command -v jq &> /dev/null; then
        echo "  📊 当前状态:"
        LAST_COUNT=$(jq -r '.last_topic_count // "未知"' "$STATE_FILE")
        LAST_CHECK=$(jq -r '.last_check_time // "未知"' "$STATE_FILE")
        
        echo "     📈 上次主题数: $LAST_COUNT"
        echo "     🕐 上次检查: $LAST_CHECK"
    fi
else
    echo "  ⚪ 状态文件不存在，可能是首次运行"
fi

echo ""

# 检查日志文件
echo "📄 日志状态:"
if [ -f "$LOG_FILE" ]; then
    LOG_SIZE=$(du -h "$LOG_FILE" | cut -f1)
    LOG_LINES=$(wc -l < "$LOG_FILE")
    echo "  ✅ 日志文件存在: $LOG_FILE"
    echo "  📏 文件大小: $LOG_SIZE"
    echo "  📝 行数: $LOG_LINES"
    
    echo "  📋 最近5条日志:"
    tail -n 5 "$LOG_FILE" 2>/dev/null | sed 's/^/     /' || echo "     (无法读取日志内容)"
else
    echo "  ⚪ 日志文件不存在"
fi

echo ""

# 检查Python环境
echo "🐍 Python环境:"
if command -v python3 &> /dev/null; then
    echo "  ✅ Python3: $(python3 --version)"
    
    # 检查关键依赖
    echo "  📦 依赖检查:"
    for pkg in requests beautifulsoup4 lxml schedule; do
        if python3 -c "import $pkg" 2>/dev/null; then
            echo "     ✅ $pkg"
        else
            echo "     ❌ $pkg (未安装)"
        fi
    done
else
    echo "  ❌ Python3 未安装"
fi

echo ""
echo "=== 状态检查完成 ==="
echo ""
echo "💡 管理命令:"
echo "  ./start_monitor.sh           # 前台启动"
echo "  ./start_scheduled_monitor.sh # 定时调度启动"
echo "  ./run_background.sh          # 后台启动"
echo "  ./stop_monitor.sh            # 停止监控"
echo "  tail -f $LOG_FILE            # 查看实时日志"
echo ""